package com.yonyou.lxp.xutils3test.network;

import android.content.Context;
import android.widget.Toast;

import com.yonyou.lxp.xutils3test.views.dialog.ShapeLoadingDialog;

import org.xutils.HttpManager;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/7/8.
 */
public class Http {
    private static Http instance = null;
    private static Context mContext;
    private static Map<String, Object> mapParams = new HashMap<>();
    private static RequestParams params;
    private static boolean isShowDialog = false;//dialog是否自动提示


    private MyCallBack callBack;
    private ShapeLoadingDialog shapeLoadingDialog;//请求数据的dialog
    private HttpManager httpUtils;
    private Callback.Cancelable cancelable;


    public Http() {
        httpUtils = x.http();
    }

    public synchronized static Http getInstance() {
        if (instance == null) {
            instance = new Http();
        }
        ClearParams();
        return instance;
    }


    /**
     * 发送请求
     *
     * @param url
     * @param callBack
     */
    public void send(String url, MyCallBack callBack, Context mContext, boolean isShowDialog) {
        this.mContext = mContext;
        this.callBack = callBack;
        this.isShowDialog = isShowDialog;
        params = new RequestParams(url);
        params.setLoadingUpdateMaxTimeSpan(1000 * 60);//设置加载更新最大时间跨度
        params.setConnectTimeout(1000 * 60);//设置连接超时
        params.setSslSocketFactory((javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault());

        if (shapeLoadingDialog == null && mContext!=null) {
                shapeLoadingDialog = new ShapeLoadingDialog(mContext);
                shapeLoadingDialog.setCanceledOnTouchOutside(false);
                shapeLoadingDialog.setLoadingText("加载中...");
        }
        for (String key : mapParams.keySet()) {
            params.addParameter(key, mapParams.get(key));
        }
        cancelable = httpUtils.post(params, new MyProgressCallback());
    }

    /**
     * 添加参数
     *
     * @param name
     * @param value
     */
    public void addParams(String name, String value) {
        mapParams.put(name, value);
    }


    /**
     * 添加参数 addQueryStringParameter
     *
     * @param name
     * @param value
     */
    public void addQueryStringParameter(String name, String value) {
        mapParams.put(name, value);
    }

    /**
     * 上传文件
     *
     * @param name
     * @param file
     */
    public void addParams(String name, File file) {
        mapParams.put(name, file);
    }


    /**
     * 回调的接口
     */
    public static abstract class MyCallBack {
        public MyCallBack() {
        }

        public abstract void callback(String result);

        public void fail(String errStr) {
        }
    }


    /**
     * 重新new  params
     */
    public static void ClearParams() {
        isShowDialog = false;
        mContext = null;
        mapParams.clear();
    }


    /**
     * 停止请求
     */
    public void cancel() {
        if (cancelable != null) {
            cancelable.cancel();
        }
    }


    /**
     * 　回调封装
     */
    class MyProgressCallback implements Callback.ProgressCallback<String> {

        @Override
        public void onSuccess(String result) {//成功
            LogUtil.e("请求成功："+result);
            if (isShowDialog && mContext != null) {
                shapeLoadingDialog.dismiss();
            }
            callBack.callback(result);
        }


        @Override
        public void onError(Throwable ex, boolean isOnCallback) {//错误
            if (isShowDialog && mContext != null) {
                shapeLoadingDialog.dismiss();
                Toast.makeText(mContext, "网络异常，请检查网络连接！", Toast.LENGTH_SHORT).show();
            }
            LogUtil.e("请求错误："+ex.getMessage() + "");
            callBack.fail(ex.getMessage());
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {//取消
            LogUtil.e("请求取消");
            if (isShowDialog && mContext != null) {
                shapeLoadingDialog.dismiss();
            }
        }

        @Override
        public void onFinished() {//结束
            LogUtil.e("onFinished" + "请求结束");
        }

        @Override
        public void onWaiting() {//等待
            LogUtil.e("请求等待");
        }

        @Override
        public void onStarted() {//开始
            LogUtil.e("请求开始");
            if (isShowDialog && mContext != null) {
                shapeLoadingDialog.show();
            }
        }

        @Override
        public void onLoading(long total, long current, boolean isDownloading) {//进度
            LogUtil.e(total + "============" + current + "============" + isDownloading);
        }
    }

}
